package com.itheima.service.empl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.bean.Category;
import com.itheima.bean.Setmeal;
import com.itheima.bean.SetmealDish;
import com.itheima.dao.SetmealDao;
import com.itheima.dto.SetmealDto;
import com.itheima.service.SetmealService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

@Service
@Transactional
public class SetmealServiceImpl implements SetmealService {

    @Lazy
    @Autowired
    private CategoryServiceImpl categoryService;

    @Autowired
    private SetmealDishServiceImpl setmealDishService;

    @Autowired
    private SetmealDao setmealDao;

    /**
     * 通过category id条件查找套餐
     *
     * @param id
     * @return
     */
    @Override
    public List<Setmeal> findCond(Long id) {
        LambdaQueryWrapper<Setmeal> qw = new LambdaQueryWrapper<>();
        qw.eq(Setmeal::getCategoryId, id);
        return setmealDao.selectList(qw);
    }

    /**
     * 添加套餐
     *
     * @param setmealDto
     * @return
     */
    @Override
    public int add(SetmealDto setmealDto) {
        int row = setmealDao.insert(setmealDto);
        List<SetmealDish> setmealDishList = setmealDto.getSetmealDishes();
        if (row > 0) {
            int row1 = 0;
            if (setmealDishList != null && setmealDishList.size() > 0) {
                for (SetmealDish setmealDish : setmealDishList) {
                    setmealDish.setSetmealId(setmealDto.getId());
                    row1 += setmealDishService.add(setmealDish);
                }
            }
            return row1 == setmealDishList.size() ? 1 : 0;
        }
        return row;
    }

    /**
     * 分页查询套餐
     *
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @Override
    public IPage<SetmealDto> findPage(int page, int pageSize, String name) {
        IPage<Setmeal> iPage = new Page<>(page, pageSize);
        LambdaQueryWrapper<Setmeal> qw = new LambdaQueryWrapper<>();
        qw.like(name != null, Setmeal::getName, name);
        IPage<Setmeal> setmealIPage = setmealDao.selectPage(iPage, qw);
        List<Setmeal> setmealList = setmealIPage.getRecords();
        List<SetmealDto> setmealDtoList = setmealList.stream().map(setmeal -> {
            Category category = categoryService.findById(setmeal.getCategoryId());
            SetmealDto setmealDto = new SetmealDto();
            setmealDto.setCategoryName(category.getName());
            BeanUtils.copyProperties(setmeal, setmealDto);
            return setmealDto;
        }).collect(Collectors.toList());
        IPage<SetmealDto> dtoIPage = new Page<>();
        dtoIPage.setRecords(setmealDtoList);
        dtoIPage.setTotal(setmealIPage.getTotal());
        return dtoIPage;
    }

    /**
     * 通过id查找套餐
     *
     * @param id
     * @return
     */
    @Override
    public SetmealDto findById(Long id) {
        Setmeal setmeal = setmealDao.selectById(id);
        SetmealDto setmealDto = new SetmealDto();
        Category category = categoryService.findById(setmeal.getCategoryId());
        List<SetmealDish> setmealDishList = setmealDishService.findBySetId(id);
        setmealDto.setCategoryName(category.getName());
        setmealDto.setSetmealDishes(setmealDishList);
        BeanUtils.copyProperties(setmeal, setmealDto);
        return setmealDto;
    }

    /**
     * 修改套餐
     *
     * @param setmealDto
     * @return
     */
    @Override
    public int update(SetmealDto setmealDto) {
        int row = setmealDao.updateById(setmealDto);
        setmealDishService.delete(setmealDto.getId());
        if (row > 0) {
            List<SetmealDish> setmealDishList = setmealDto.getSetmealDishes();
            int row1 = 0;
            if (setmealDishList != null && setmealDishList.size() > 0) {
                for (SetmealDish setmealDish : setmealDishList) {
                    setmealDish.setSetmealId(setmealDto.getId());
                    row1 += setmealDishService.add(setmealDish);
                }
            }
            return row1 == setmealDishList.size() ? 1 : 0;
        }
        return row;
    }

    /**
     * 删除套餐
     *
     * @param ids
     * @return
     */
    @Override
    public int delete(List<Long> ids) {
        LambdaQueryWrapper<Setmeal> qw = new LambdaQueryWrapper<>();
        qw.eq(Setmeal::getStatus, 1);
        qw.in(Setmeal::getId, ids);
        int row = setmealDao.selectCount(qw);
        if (row > 0) {
            return -1;
        }
        int row1 = setmealDao.deleteBatchIds(ids);
        for (Long id : ids) {
            setmealDishService.delete(id);
        }
        return row1;
    }

    /**
     * 修改套餐成起售状态
     *
     * @param ids
     * @return
     */
    @Override
    public int updateStart(List<Long> ids) {
        List<Setmeal> setmeals = setmealDao.selectBatchIds(ids);
        int row=0;
        for (Setmeal setmeal : setmeals) {
            setmeal.setStatus(1);
          row += setmealDao.updateById(setmeal);
        }
        return row;
    }

    /**
     * 修改套餐成停售状态
     *
     * @param ids
     * @return
     */
    @Override
    public int updateStop(List<Long> ids) {
        List<Setmeal> setmeals = setmealDao.selectBatchIds(ids);
        int row=0;
        for (Setmeal setmeal : setmeals) {
            setmeal.setStatus(0);
            row += setmealDao.updateById(setmeal);
        }
        return row;
    }

    /**
     * 回显数据到手机端
     *
     * @param categoryId
     * @param status
     * @return
     */
    @Override
    public List<SetmealDto> list(Long categoryId, Integer status) {
        LambdaQueryWrapper<Setmeal> qw = new LambdaQueryWrapper<>();
        qw.eq(Setmeal::getCategoryId,categoryId);
        qw.eq(Setmeal::getStatus,status);
        List<Setmeal> setmealList = setmealDao.selectList(qw);
       return setmealList.stream().map(setmeal -> {
            List<SetmealDish> setmealDishList = setmealDishService.findBySetId(setmeal.getId());
            SetmealDto setmealDto = new SetmealDto();
            setmealDto.setSetmealDishes(setmealDishList);
            BeanUtils.copyProperties(setmeal,setmealDto);
            return setmealDto;
        }).collect(Collectors.toList());
    }
}
